package com.tk.exam.web.admin;

import com.alibaba.fastjson.JSONObject;
import com.tk.exam.domain.TkQuestionCategory;
import com.tk.exam.exception.ApiException;
import com.tk.exam.http.Response;
import com.tk.exam.service.TkExamPaperManagerService;
import com.tk.exam.service.TkExamPaperService;
import com.tk.exam.service.TkQuesCategoryService;
import com.tk.exam.utils.JsonUtils;
import com.tk.exam.utils.PageInfo;
import com.tk.exam.vo.paper.ExamPaperRequestVo;
import com.tk.exam.vo.paper.ExamPaperVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;


@RestController
@RequestMapping("tk/exam/admin/examPaperManger")
@Slf4j
public class TkExamPaperController {
    @Autowired
    private TkExamPaperService tkExamPaperService;
    @Autowired
    private TkExamPaperManagerService tkExamPaperManagerService;
    /**
     * 分页查询以WithPage结尾
     *
     * @param :
     * @return
     */
    @RequestMapping(value = "/searchExamPaperList", method = RequestMethod.POST)
    public Response searchExamPaperList() {
        List<ExamPaperVo> examPaperVos = tkExamPaperService.searchExamPapers();
        return Response.succeed().put("examPaperList",examPaperVos);
    }



    /**
     * 新增试卷
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/addExamPaper", method = RequestMethod.POST)
    public Response addExamPaper(@RequestBody ExamPaperRequestVo vo) {
//        ExamPaperRequestVo vo = JsonUtils.string2Object(param.toJSONString(),ExamPaperRequestVo.class);
        if(StringUtils.isBlank(vo.getPaperName())){
            return Response.fail("试卷名称不能为空");
        }
        tkExamPaperManagerService.save(vo);
        return Response.succeed();
    }
    @RequestMapping(value = "/editExamPaper",method = RequestMethod.POST)
    public Response editExamPaper(@RequestBody ExamPaperRequestVo vo){
//        ExamPaperRequestVo vo = JsonUtils.string2Object(param.toJSONString(),ExamPaperRequestVo.class);
        if(StringUtils.isBlank(vo.getId())){
            return Response.fail("id不能为空");
        }
        if(StringUtils.isBlank(vo.getPaperName())){
            return Response.fail("试卷名称不能为空");
        }
        tkExamPaperManagerService.save(vo);
        return Response.succeed();
    }

    @RequestMapping(value = "/findExamPaperById",method = RequestMethod.GET)
    public Response findExamPaperById(@RequestParam String id){
        if(StringUtils.isBlank(id)){
            return Response.fail("入参错误！");
        }
        //组装返回数据
        ExamPaperRequestVo vo = tkExamPaperManagerService.findExamPaperInfoById(id);
        return Response.succeed(vo);
    }

    @RequestMapping(value = "/deleteExamPapers",method = RequestMethod.POST)
    public Response deleteExamPaperById(String[] examPaperIds){
        if(examPaperIds==null||examPaperIds.length==0){
            return Response.fail("试卷id不能为空！");
        }
        List<String> idlist = Arrays.asList(examPaperIds);
        List<Integer> intIdList = idlist.stream().map(id->Integer.valueOf(id)).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(intIdList)){
            throw ApiException.of("ids不能为空");
        }
        tkExamPaperManagerService.deleteExamPapers(intIdList);
        //组装返回数据
        return Response.succeed();
    }


}
